package Service;

import DAO.PurchaseDao;
import Entities.PurchaseEntity;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PaymentService {
    private PurchaseDao purchaseDao;

    public PurchaseDao getPurchaseDao() {
        return purchaseDao;
    }

    public void setPurchaseDao(PurchaseDao purchaseDao) {
        this.purchaseDao = purchaseDao;
    }

    //处理扣款
    public String paymentSolver(String user_name,float totalPrice){
        float balance = purchaseDao.getBalance(user_name);
        boolean flag = false;
        if (balance >= totalPrice) {
            flag = purchaseDao.updateBalance("decrease", user_name, totalPrice);
            return "success";
        } else
            return "insufficient balance";
    }

    public String itemHandler(String book_id, String user_name, String count,String purchaseNo,Date date)
    {

        boolean flag = purchaseDao.addPurchaseItem(user_name,book_id,purchaseNo,date,count);

        if(flag)
            return "success";
        else
            return "fail";
    }

    public String PNoGenerator(long time,String user_name){
        String no = user_name.hashCode() + String .valueOf(time);
        return no;
    }

    //处理添加订单记录
    public String addPurchaseItem(String user_name)
    {
        Cache defaultCache = CacheManager.getCacheManager("__DEFAULT__").getCache("DefaultCache");
        Element element = defaultCache.get(user_name);
        String dataJson = (String) element.getObjectValue();
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        String purchaseNo = PNoGenerator(time,user_name);

        List<PurchaseItem> purchaseItemList = jsonParser(dataJson);
        for(PurchaseItem item:purchaseItemList)
        {
            String book_id = item.bookId;
            String count = String.valueOf(item.count);
            String result = itemHandler(book_id,user_name,count,purchaseNo,date);
            if (!result.equals("success"))
                return "fail";

        }

        return "success";
    }

    public List<PurchaseEntity> purchaseItemQuery(String user_name)
    {
        List<PurchaseEntity> list = purchaseDao.purchaseItemQuery(user_name);
        return list;
    }

    public List<PurchaseItem> jsonParser(String jsonSrc){
        List <PurchaseItem> itemList = new ArrayList<>();
        try {
            List<Map> list = (List) JSONUtil.deserialize(jsonSrc);
            for (Map<String,String> itemMap : list){
                PurchaseItem item = new PurchaseItem() ;
                item.bookId = itemMap.get("bookId");
                item.count = Integer.parseInt(itemMap.get("count"));
                itemList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return itemList;
    }
    private class PurchaseItem{
        String bookId;
        int count;
    }

}
